package com.newlink.building.main.mvp.presenter

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.OnLifecycleEvent
import com.newlink.building.common_base.utils.NLog
import com.newlink.building.common_base.base.BasePresenter
import com.newlink.building.common_base.ext.ss
import com.newlink.building.common_base.model.bean.DeviceListRequest
import com.newlink.building.common_base.model.bean.OpenDoorRequest
import com.newlink.building.main.mvp.contract.Module_Main_HomeContract
import com.newlink.building.main.mvp.model.Module_Main_HomeModel

/**
 * Created by xiaodong on 2022/7/25.
 */
class Module_Main_HomePresenter : BasePresenter<Module_Main_HomeContract.Model, Module_Main_HomeContract.View>(),
    Module_Main_HomeContract.Presenter {
    //
    private var isResume = false
    override fun createModel(): Module_Main_HomeContract.Model = Module_Main_HomeModel()

    override fun getDeviceList(request: DeviceListRequest) {
        mModel?.getDeviceList(request)?.ss(mModel, mView) {
            if (it != null) {
                NLog.d("getDeviceList ${it.data}")
                if (it.data != null && it.data.list != null) {
                    mView?.refreshDeviceList(it.data.total, it.data.list)
                }
                mView?.hideLoading()
            } else {
                NLog.d("getDeviceList111 data is null")
            }
        }
    }

    override fun getDoorInfo(token: String, mac: String) {
        mModel?.getDoorInfo(token, mac)?.ss(mModel, mView) {
            if (it != null) {
                NLog.d("getDoorInfo ${it.data}")
                if (it.data != null) {
                    mView?.refreshDoorInfo(it.data)
                }
            }
        }
    }

    override fun openDoorAction(request: OpenDoorRequest) {
        mModel?.openDoorAction(request)
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    open fun onResume(owner: LifecycleOwner) {
        isResume = true
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    open fun onPause(owner: LifecycleOwner) {
        isResume = false
    }

    override fun getMissedCalls(token: String) {
        mModel?.getMissedCalls(token)?.ss(mModel, mView) { logoutData ->
            NLog.e("getMissedCalls response ->  ${logoutData?.code}")
            val count = logoutData?.data ?: 0
            mView?.getMissedCallsSuccess(count > 0)
        }
    }

    override fun getUnreadFlag(token: String, type: Int) {
        mModel?.getUnreadFlag(token, type)?.ss(mModel, mView) {
            if (it?.code == 200 && it?.data != null) {
                val msgNum = it.data.msgNum ?: 0
                val noticeNum = it.data.noticeNum ?: 0
                val unread = it?.data!!.msgNum + it?.data!!.noticeNum
                mView?.getUnreadFlagSuccess(unread > 0, msgNum, noticeNum)
            }
        }
    }

    override fun getRecordUnreadPrompt(token: String, prjId: Long) {
        mModel?.getRecordUnreadPrompt(token, prjId)?.ss(mModel, mView, false) {
            if (it?.code == 200 && it?.data != null) {
                mView?.getRecordUnreadPromptSuccess(it.data)
            }
        }
    }

    override fun getPrjList(token: String) {
        mModel?.getPrjList(token)?.ss(mModel, mView) {
            if (it?.code == 200 && it?.data != null) {
                mView?.getPrjListSuccess(it.data)
            }
        }
    }
}
